package {
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	
	import com.googlecode.milib.animation.dynamic_.AnimatedInertialNumber;
	import com.googlecode.milib.animation.dynamic_.AnimatedNumber;
	import com.googlecode.milib.animation.dynamic_.AnimatedStoppingNumber;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLActionClass;
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.dbg.InstanceDebugServiceProxy;
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.vo.MLEventInfo;		

	/**
	 * @author Marek Brun 'minim'
	 */
	public class AandBDynamic extends MLBaseClass implements IMLBroadcasterListener {

		private var mlmc:MLMC;
		private var mc_ball:MovieClip;
		private var mc_a:MovieClip;
		private var mc_b:MovieClip;
		private var animXToA:AnimatedInertialNumber;
		private var animYToA:AnimatedInertialNumber;
		private var ks:KeyboardService;
		private var animXToB:AnimatedStoppingNumber;
		private var animYToB:AnimatedStoppingNumber;

		public function AandBDynamic(mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			
			mc_ball=mlmc.getMC('ball');
			mc_a=mlmc.getMC('a');
			mc_b=mlmc.getMC('b');
			
			animXToA=new AnimatedInertialNumber();
			animXToA.isFinishWhenNoMapping=true;			animXToA.addAutoMapping(mc_ball, 'x', true);
			animXToA.target=mc_a.x;
			animXToA.addListener(this);
			
			animYToA=new AnimatedInertialNumber();
			animYToA.isFinishWhenNoMapping=true;
			animYToA.addAutoMapping(mc_ball, 'y', true);
			animYToA.target=mc_a.y;
			animYToA.addListener(this);
			
			
			animXToB=new AnimatedStoppingNumber();
			animXToB.isFinishWhenNoMapping=true;
			animXToB.addAutoMapping(mc_ball, 'x', true);
			animXToB.target=mc_b.x;
			animXToB.addListener(this);
			
			animYToB=new AnimatedStoppingNumber();
			animYToB.isFinishWhenNoMapping=true;
			animYToB.addAutoMapping(mc_ball, 'y', true);
			animYToB.target=mc_b.y;			animYToB.addListener(this);
			
			ks=KeyboardService.getInstance();
			ks.addListener(this);
			
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
//********************************************************************************************
//		events for AandBDynamic
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case ks:
					switch(ev.event) {
						case ks.event_Down:
							switch(KeyboardEvent(ev.data).keyCode) {
								case KeyboardService.KEY_A:
									MLDBG.log('A');
									animXToA.start();
									animYToA.start();
								break;
								case KeyboardService.KEY_B:
									MLDBG.log('B');
									animXToB.start();
									animYToB.start();
								break;
							}
						break;
					}
				break;
				case animXToA:
					switch(ev.event) {
						case animXToA.event_ActionFinish:
							dbg.log('animXToA.event_ActionFinish');
						break;
					}
				break;
				case animYToA:
					switch(ev.event) {
						case animYToA.event_ActionFinish:
							dbg.log('animYToA.event_ActionFinish');
						break;
					}
				break;
				case animXToB:
					switch(ev.event) {
						case animXToB.event_ActionFinish:
							dbg.log('animXToB.event_ActionFinish');
						break;
					}
				break;
				case animYToB:
					switch(ev.event) {
						case animYToB.event_ActionFinish:
							dbg.log('animYToB.event_ActionFinish');
						break;
					}
				break;
			}
		}
		
	}
}
